package com.powernode.driver.service.impl;


import com.powernode.common.constant.RedisConstant;
import com.powernode.common.execption.PowerException;
import com.powernode.common.result.ResultCodeEnum;
import com.powernode.dispatch.client.NewOrderFeignClient;
import com.powernode.driver.client.DriverInfoFeignClient;
import com.powernode.driver.service.DriverService;
import com.powernode.map.client.LocationFeignClient;
import com.powernode.model.form.driver.DriverFaceModelForm;
import com.powernode.model.form.driver.UpdateDriverAuthInfoForm;
import com.powernode.model.vo.driver.DriverAuthInfoVo;
import com.powernode.model.vo.driver.DriverLoginVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class DriverServiceImpl implements DriverService {

    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private LocationFeignClient locationFeignClient;

    @Resource
    private NewOrderFeignClient newOrderFeignClient;

    /**
     * 配送员登录
     */
    @Override
    public String login(String code) {

        //配送员id
        Long driverId = driverInfoFeignClient.login(code).getData();

        String token = UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,driverId.toString());

        return token;
    }


    /**
     * 获取配送员信息
     */
    @Override
    public DriverLoginVo getDriverLoginInfo(Long driverId) {
        return driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
    }

    /**
     * 获取配送员认证信息
     */
    @Override
    public DriverAuthInfoVo getDriverAuthInfo(Long driverId) {
        return driverInfoFeignClient.getDriverAuthInfo(driverId).getData();
    }

    /**
     * 更新配送员认证信息
     */
    @Override
    public Boolean updateDriverAuthInfo(UpdateDriverAuthInfoForm updateDriverAuthInfoForm) {
        return driverInfoFeignClient.updateDriverAuthInfo(updateDriverAuthInfoForm).getData();
    }

    /**
     * 创建配送员人脸模型
     */
    @Override
    public Boolean createDriverFaceModel(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.createDriverFaceModel(driverFaceModelForm).getData();
    }

    /**
     * 判断配送员当天是否进行人脸识别
     */
    @Override
    public Boolean isFaceRecognition(Long driver) {
        return driverInfoFeignClient.isFaceRecognition(driver).getData();
    }

    /**
     * 验证配送员人脸
     */
    @Override
    public Boolean verifyDriverFace(DriverFaceModelForm driverFaceModelForm) {
        return driverInfoFeignClient.verifyDriverFace(driverFaceModelForm).getData();
    }

    /**
     * 开始接单
     */
    @Override
    public Boolean startService(Long driverId) {
        //判断认证状态
        DriverLoginVo driverLoginVo = driverInfoFeignClient.getDriverLoginInfo(driverId).getData();
        if (driverLoginVo.getAuthStatus() != 2) {
            throw new PowerException(ResultCodeEnum.AUTH_ERROR);
        }

        //判断是否进行人脸识别
        Boolean isFaceRecognition = driverInfoFeignClient.isFaceRecognition(driverId).getData();
        if (!isFaceRecognition) {
            throw new PowerException(ResultCodeEnum.FACE_ERROR);
        }

        //更新配送员接单状态
        driverInfoFeignClient.updateServiceStatus(driverId, 1);

        //删除配送员的位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空配送员redis list信息
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }

    /**
     * 结束接单
     */
    @Override
    public Boolean stopService(Long driverId) {
        driverInfoFeignClient.updateServiceStatus(driverId, 0);
        //删除配送员的位置信息
        locationFeignClient.removeDriverLocation(driverId);
        //清空配送员redis list信息
        newOrderFeignClient.clearNewOrderQueueData(driverId);

        return true;
    }
}
